* sample_choice.do	JEP 01/23/15
* This file builds on prelim_est*.do, builds a program to choose the sample to run regressions

* 1. Choose a sample and a measure
* 2. Calculate minimum wage measures
* 3. Create treatment variables and trends
* 4. Run regressions

*************************** Change log ********************************** 
/*
	01/25/15	JEP		Exclude observations that have salario=. from sample. Regressions do this anyway but it's better to do it early on.
						Replace with horas <= max, to get more sample, now that I have confirmed that the code replicates the regressions.
						There was a mistake on sample selection for RIF11 which is now fixed.
	04/05/15	JEP		Add industry choice
*/
cap program drop mysample
program mysample
	syntax newvarname , hmin(integer) hmax(integer) sex(integer) edadm(integer) wage(varname) [domestic formal informal exind(numlist)]
	tempvar horas_s sex_s edad_s cat hasw
	
	if "`exind'"!=""{
		local cexind : subinstr local exind " " ",", all
		keep if !inlist(cactividad_empresa,`cexind')
	}
	
	gen `horas_s'=(horas_semana>=`hmin' & horas_semana <=`hmax')
	if `sex'==0 gen `sex_s'=1
	else gen `sex_s' = (sex==`sex')
	gen `edad_s' = (cedad<=`edadm')
	
	* If no sample choice, include all
	if "`domestic'"=="" & "`formal'"=="" & "`informal'"=="" gen `cat'=1
	else {
		* Include formal if formal is on
		if "`formal'"=="formal" gen `cat' = (afiliado_salud==1) | (afiliado_salud==. & cubierto_ss==1)
		* Include informal if informal is on
		else if "`informal'"=="informal" gen `cat' = (afiliado_salud==2) | (afiliado_salud==. & cubierto_ss==2)
		* By default exclude domestic, include only if called. Include ALL domestic, not only those with salud==2
		if "`domestic'"=="domestic" replace `cat' =1 if `cat'==0 & (tipo_trabajador==4)
		else replace `cat' =0 if `cat'==1 & (tipo_trabajador==4)
	}
	* This drops afiliado_salud=9
	
	* Salario!=.
	gen `hasw' = (`wage'!=.)
	
	* Generate sample variable
	
	gen `varlist' =  (`horas_s' & `sex_s' & `edad_s' & `cat' & `hasw')
end
	





